﻿using System;
using Hopscotch.Common.Helpers;
using Hopscotch.Common.Contracts.AppService;
using Hopscotch.Domain.Common.Enums;
using Hopscotch.Domain.Finance;

namespace Hopscotch.Server.AppServiceFake
{
    public class TradeService : FakeServiceIntKey<Trade>, ITradeService
    {
        private readonly IInstrumentService instrumentService;
        private static bool isInitialized;

        public TradeService(IInstrumentService instrumentService)
        {
            this.instrumentService = instrumentService;
            Initialize();
        }

        private void Initialize()
        {
            if (isInitialized)
                return;

            DateTime today = DateTime.Now.Date;
            DateTime valueDt = DateTimeHelper.AddWeekDays(today, 3);

            Add(new Trade { Id = 1000, InstrumentId = 1000, Qty = 1000, Price = 101, Activity = TradeActivity.Buy, Memo = "COD" });
            Add(new Trade { Id = 1001, InstrumentId = 1001, Qty = 2000, Price = 102, Activity = TradeActivity.Buy, Memo = "COD" });
            Add(new Trade { Id = 1002, InstrumentId = 1002, Qty = 3000, Price = 103, Activity = TradeActivity.Buy, Memo = "Urgent" });
            Add(new Trade { Id = 1003, InstrumentId = 1003, Qty = 4000, Price = 104, Activity = TradeActivity.Buy, Memo = "Urgent" });
            Add(new Trade { Id = 1004, InstrumentId = 1004, Qty = 5000, Price = 105, Activity = TradeActivity.Buy, Memo = "Best price only" });
            Add(new Trade { Id = 1005, InstrumentId = 1005, Qty = 6000, Price = 106, Activity = TradeActivity.Buy, Memo = "Best price only" });

            Add(new Trade { Id = 1006, InstrumentId = 1006, Qty = 5000, Price = 110, Activity = TradeActivity.Buy });
            Add(new Trade { Id = 1007, InstrumentId = 1007, Qty = 2500, Price = 120, Activity = TradeActivity.Sell });

            foreach (Trade trade in FakeData.Values)
            {
                trade.TradeDt = today;
                trade.ValueDt = valueDt;
                if (trade.InstrumentId.HasValue)
                {
                    var equity = instrumentService.Get(trade.InstrumentId.Value) as EquityInstrument;
                    trade.TradeCcy = equity.DefaultCurrency;
                }

            }

            ResetObjectState();
            isInitialized = true;
        }

    }
}
